首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IPTV播放器在Android中的应用

IPTV播放器在Android中的应用
EN

Stack Overflow用户
提问于 2011-05-18 12:48:02
回答 2查看 3.6K关注 0票数 1

我知道有许多免费的iptv频道可供观看。因此,我想开发一个应用程序来播放这些多播流,使用我的android媒体播放器。

有人能指点我吗。

谢谢,

Sen

EN

回答 2

Stack Overflow用户

发布于 2017-02-03 12:56:20

我以前就这么做过..。如果只想播放多播流,请将VideoView添加到MainActivity中。例如:

代码语言:javascript
复制
 VideoView videoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        videoView = (VideoView) findViewById(R.id.videoView);
        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                selectedVideo++;
                play();
            }
        });

        play();
    }

并播放功能代码:

代码语言:javascript
复制
private void play() {
    String result = "udp://239.1.2.3:6001";

Uri video = Uri.parse(result);

if (videoView.isPlaying())
    videoView.stopPlayback();

videoView.setVideoURI(video);
videoView.requestFocus();
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mediaPlayer) {
        videoView.start();
    }
});
}

请注意,必须使具有启用组播的网络来播放多播流。

票数 1
EN

Stack Overflow用户

发布于 2019-10-23 17:31:13

我用ExoMedia开发了一个个人示例。这里是链接,但基本上是一个服务和PlaylistManager的组合,用于播放多播流

代码语言:javascript
复制
// PlaylistManager.kt
/**
 * A PlaylistManager that extends the [ListPlaylistManager] for use with the
 * [MediaService] which extends [com.devbrackets.android.playlistcore.service.BasePlaylistService].
 */
class PlaylistManager(application: Application) :
    ListPlaylistManager<ChannelItem>(application, MediaService::class.java) {

    /**
     * Note: You can call [.getMediaPlayers] and add it manually in the activity,
     * however we have this helper method to allow registration of the media controls
     * listener provided by ExoMedia's [com.devbrackets.android.exomedia.ui.widget.VideoControls]
     */
    fun addVideoApi(videoApi: VideoApi) {
        mediaPlayers.add(videoApi)
        updateVideoControls(videoApi)
        registerPlaylistListener(videoApi)
    }

    /**
     * Note: You can call [.getMediaPlayers] and remove it manually in the activity,
     * however we have this helper method to remove the registered listener from [.addVideoApi]
     */
    fun removeVideoApi(videoApi: VideoApi) {
        videoApi.videoView.videoControls?.setButtonListener(null)

        unRegisterPlaylistListener(videoApi)
        mediaPlayers.remove(videoApi)
    }

    /**
     * Updates the available controls on the VideoView and links the
     * button events to the playlist service and this.
     *
     * @param videoApi The VideoApi to link
     */
    private fun updateVideoControls(videoApi: VideoApi) {
        videoApi.videoView.videoControls?.let {
            it.setPreviousButtonRemoved(true)
            it.setNextButtonRemoved(true)
            it.setButtonListener(ControlsListener())
        }
    }

    /**
     * An implementation of the [VideoControlsButtonListener] that provides
     * integration with the playlist service.
     */
    private inner class ControlsListener : VideoControlsButtonListener {
        override fun onPlayPauseClicked(): Boolean {
            invokePausePlay()
            return true
        }

        override fun onPreviousClicked(): Boolean {
            invokePrevious()
            return false
        }

        override fun onNextClicked(): Boolean {
            invokeNext()
            return false
        }

        override fun onRewindClicked(): Boolean {
            return false
        }

        override fun onFastForwardClicked(): Boolean {
            return false
        }
    }
}
代码语言:javascript
复制
// MediaService.kt
/**
 * A simple service that extends [BasePlaylistService] in order to provide
 * the application specific information required.
 */
class MediaService : BasePlaylistService<ChannelItem, PlaylistManager>() {

    override val playlistManager by lazy { (applicationContext as App).playlistManager }

    override fun onCreate() {
        super.onCreate()

        // Adds the audio player implementation, otherwise there's nothing to play media with
        playlistManager.mediaPlayers.add(AudioApi(applicationContext))
    }

    override fun onDestroy() {
        super.onDestroy()

        // Releases and clears all the MediaPlayersMediaImageProvider
        playlistManager.mediaPlayers.forEach {
            it.release()
        }

        playlistManager.mediaPlayers.clear()
    }

    override fun newPlaylistHandler(): PlaylistHandler<ChannelItem> {
        val imageProvider = MediaImageProvider(applicationContext) {
            playlistHandler.updateMediaControls()
        }

        return DefaultPlaylistHandler.Builder(
            applicationContext,
            javaClass,
            playlistManager,
            imageProvider
        ).build()
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6044889

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档