Android uses MediaPlayer to play streaming media, support remote and local streaming, one line of code implementation

Source: Internet
Author: User

Hdmediaplayer


This kind of package relies on Google MediaPlayer two times package.

Google China API Link: https://developer.android.google.cn/guide/topics/media/mediaplayer.html

Directory
    • How to import to a project
    • How to use
    • About Me
    • License

How to import to a project

Support Jcenter mode import.

Support local module mode import.

Jcenter Mode Import
    • Add the dependencies in the Build.gradle in the module that needs to use this library
{    compile ‘com.yhd.hdmediaplayer:hdmediaplayer:1.0.1‘}
Module Mode Import
    • Download the entire project, copy the Hdmediaplayer to the project root directory, and add the Settings.gradle
include‘:hdmediaplayer‘
    • Add the dependencies in the Build.gradle in the module that needs to use this library
dependencies {    project(‘:hdmediaplayer‘)}

How to use

This class supports the playback of music (. mp3,. wav,. ogg) and video (. mp4,. 3gp) as well as the corresponding format of network streaming media.

If you want to play a video, you need the control: Surfaceview.

    • A line of code is implemented to play the MP4 in the assets directory:
MediaPlayerHelper.getInstance().setSurfaceView((SurfaceView)findViewById(R.id.surfaceView)).playAsset(MainActivity.this,"test.mp4");
    • A line of code is implemented to play the MP3 in the assets directory:
MediaPlayerHelper.getInstance().playAsset(MainActivity.this,"test.mp3");
    • A line of code to play network streaming media or local full-channel runoff media:
/** * urlString 可为网络流媒体连接,也可以为本地存储全路径链接 */MediaPlayerHelper.getInstance().setSurfaceView((SurfaceView)findViewById(R.id.surfaceView)).play(urlString);
    • If you want a detailed callback message:
MediaPlayerHelper.getInstance().setMediaPlayerHelperCallBack(new MediaPlayerHelper.MediaPlayerHelperCallBack() {    @Override    publicvoidonCallBack(MediaPlayerHelper.CallBackState state, MediaPlayerHelper mediaPlayerHelper, Object... args) {        Log.v(TAG,"--"+state.toString());        if(state== MediaPlayerHelper.CallBackState.PROGRESS){            int percent=(int)args[0];            Log.v(TAG,"--progress:"+percent);        }    }});
    • For more usage please see demo:
/** 设置播放进度回调时间间隔 */MediaPlayerHelper.getInstance().setProgressInterval(msec);/** 获得MediaPlayer对象,进行更多的自定义的操作 */MediaPlayerHelper.getInstance().getMediaPlayer().pause();MediaPlayerHelper.getInstance().getMediaPlayer().start();MediaPlayerHelper.getInstance().getMediaPlayer().stop();

About Me
欢迎 Star Fork交流地址:尹海德([email protected])

License
Copyright 2017 YinhaideLicensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at   http://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.
GitHub Portal Https://github.com/yinhaide/HDMediaPlayer

Android uses MediaPlayer to play streaming media, support remote and local streaming, one line of code implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.