一个简单的Android命令行播放器

利用:
mediaplayer和SurfaceComposerClient

在native层创建surface,然后指定url进行播放。

环境: Android 8.1 rk3399pro

例如:
TestMediaserver http://172.18.10.76/1.ts

目录结构:

frameworks/av/media/test_player
├── Android.mk
└── TestMediaPlayer.cpp

TestMediaPlayer.cpp:

#define LOG_TAG "test_mediaserver"
#define LOG_NDEBUG 0

#include <media/mediaplayer.h>
#include <media/IMediaHTTPService.h>
#include <gui/Surface.h>
#include <gui/SurfaceComposerClient.h>
#include <gui/ISurfaceComposer.h>
#include <ui/DisplayInfo.h>
#include <ui/Rect.h>
#include <ui/Region.h>
#include <android/native_window.h>

using namespace android;

bool gIsEnd;

class TestMediaPlayerListener : public MediaPlayerListener
{
public:
    TestMediaPlayerListener(sp<MediaPlayer>& player):mp(player) {}
    void notify(int msg, int ext1, int ext2, const Parcel *obj)
    {
        (void)msg;
        (void)ext1;
        (void)ext2;
        (void)obj;
        
        switch (msg) 
        {
        
            case MEDIA_INFO:
            {
                ALOGE("MEDIA_INFO,  ext1 : %d, ext2 : %d ", ext1, ext2);
                if (MEDIA_INFO_RENDERING_START == ext1) {
                    ALOGE("begin to rendering frame now!!!!!!!!!");
                }
            }
            break;
            
            case MEDIA_PREPARED:
            {
                ALOGE("MEDIA_PREPARED,  ext1 : %d, ext2 : %d ", ext1, ext2);
                if (mp.get()) 
                {
                    gIsEnd = false;
                    mp->start();
                }
            }
            break;
            
            case MEDIA_ERROR:
                ALOGE("MEDIA_ERROR,  ext1 : %d, ext2 : %d ", ext1, ext2);
            break;
            
            case MEDIA_PLAYBACK_COMPLETE:
            {
                ALOGE("MEDIA_PLAYBACK_COMPLETE,  ext1 : %d, ext2 : %d ", ext1, ext2);
                gIsEnd = true;
            }
            break;
            
            case MEDIA_STOPPED:
            {
                ALOGE("MEDIA_STOPPED,  ext1 : %d, ext2 : %d ", ext1, ext2);
            }
            break;
            
            default:
                break;
        }
        
    }
    
private:
    sp<MediaPlayer>  mp;
};

int main (int argc __unused, char **argv __unused) {
    
    if (argc < 2)
    {
        printf("Use  TestMediaserver  http://xxxxx \n");
        return 0;
    }
    
    int argv_index = 0;
    for(; argv_index<argc; argv_index++)
    {
         printf("argv[%d]: %s \n", argv_index, argv[argv_index]);
    }
    
    String8  url(argv[1]);
    
    bool isStreaming = false;
    if (url.contains("http://") || url.contains("https://"))
    {
        printf("This is a streaming playback url: %s\n", url.string());
        isStreaming = true;
    }
    else
    {
        printf("local file \n");
    }
    
    sp<MediaPlayer> mp = new MediaPlayer();
    
    if (true == isStreaming)
    {
        sp<IMediaHTTPService> httpService = NULL;
        mp->setDataSource(
                    httpService,
                    url.string(),
                    NULL);
    }
    else
    {
        // open fd in here
    }
    
    // create a client to surfaceflinger
    sp<SurfaceComposerClient> client = new SurfaceComposerClient();
    
    sp<SurfaceControl> surfaceControl = 
    client->createSurface( String8("resize"), 
    800, 
    600, 
    PIXEL_FORMAT_RGB_565, 0);
    
    sp<Surface> surface = surfaceControl->getSurface();
    SurfaceComposerClient::openGlobalTransaction();
    surfaceControl->setLayer(100000);
    SurfaceComposerClient::closeGlobalTransaction();
    
    mp->setVideoSurfaceTexture( surface->getIGraphicBufferProducer() );
    
    sp<TestMediaPlayerListener> listener = new TestMediaPlayerListener(mp);
    
    mp->setListener(listener);
    
    mp->prepareAsync();
    
    while(1)
    {
        if (true == gIsEnd)
        {
            ALOGE("Exit!!!!!!!!!!!!!!!");
            break;
        }
        usleep(1000);
    }
    
    SurfaceComposerClient::openGlobalTransaction();
    surfaceControl->clear();
    SurfaceComposerClient::closeGlobalTransaction();
    
    mp->reset();
    ALOGE(" End !!!!!!!!!!");
    
    return 0;
}

Android.mk

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_SRC_FILES:= \
        TestMediaPlayer.cpp

LOCAL_SHARED_LIBRARIES := \
        liblog \
        libmediaplayerservice \
        libmedia \
        libutils \
        libbinder \
        libgui \

LOCAL_C_INCLUDES := \
        frameworks/av/media/libmediaplayerservice \
        frameworks/av/include \
        frameworks/native/libs/gui/include \
        frameworks/native/libs/nativewindow/include \

LOCAL_MODULE:= TestMediaserver
LOCAL_32_BIT_ONLY := true

LOCAL_CFLAGS := -Werror -Wall

include $(BUILD_EXECUTABLE)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tui_GuiGe

鼓励一下作者吧,请他喝一瓶啤酒

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值