1. 程式人生 > >使用ijkplayer進行視訊播放

使用ijkplayer進行視訊播放

ijkplayer可以支援網路和本地的視訊播放,注意路徑就可以了
先來看一下程式碼吧,裡面添加了音量,亮度調節,只限在這個播放頁面內,我感覺這是合理的,不要在自己的應用內動了使用者的手機的配置又不去復原。
先來上一張圖:動圖太大了,上傳失敗,就放了一張png
這裡寫圖片描述
activity:

package com.fanyafeng.testijkplayer.activity;

import android.content.Context;
import android.content.res.AssetManager;
import android.media.AudioManager;
import
android.os.Bundle; import android.os.Handler; import android.os.Message; import android.provider.Settings; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import
android.view.DragEvent; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.SeekBar; import android.widget.TextView; import com.fanyafeng.testijkplayer.R; import com.fanyafeng.testijkplayer.BaseActivity; import com.fanyafeng.testijkplayer.fragment.TracksFragment; import
com.fanyafeng.testijkplayer.util.FitScreenUtil; import com.fanyafeng.testijkplayer.util.MyUtils; import com.fanyafeng.testijkplayer.widget.media.AndroidMediaController; import com.fanyafeng.testijkplayer.widget.media.IjkVideoView; import tv.danmaku.ijk.media.exo.IjkExoMediaPlayer; import tv.danmaku.ijk.media.player.IjkMediaPlayer; import tv.danmaku.ijk.media.player.misc.ITrackInfo; //需要搭配baseactivity,這裡預設為baseactivity,並且預設Baseactivity為包名的根目錄 public class PlayerActivity extends BaseActivity implements TracksFragment.ITrackHolder { private boolean backPressed; private IjkVideoView videoView; private TextView tvVideoInfo; private TextView tvPlayTime; private TextView tvCurrentTime; private TextView tvTotalTime; private SeekBar seekBarVideo; private SeekBar seekBarSound; private SeekBar seekBarLight; private int screenBrightness; private int screenMode; private AudioManager audioManager; private AssetManager assetManager; private int soundAudio; private int soundMode; private AndroidMediaController mediaController; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_player); title = getString(R.string.title_activity_player); mediaController = new AndroidMediaController(this, false); try { screenBrightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS); screenMode = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); soundAudio = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); soundMode = audioManager.getMode(); initView(); initData(); } //初始化UI空間 private void initView() { IjkMediaPlayer.loadLibrariesOnce(null); IjkMediaPlayer.native_profileBegin("libijkplayer.so"); tvVideoInfo = (TextView) findViewById(R.id.tvVideoInfo); videoView = (IjkVideoView) findViewById(R.id.videoView); tvPlayTime = (TextView) findViewById(R.id.tvPlayTime); tvCurrentTime = (TextView) findViewById(R.id.tvCurrentTime); tvTotalTime = (TextView) findViewById(R.id.tvTotalTime); seekBarVideo = (SeekBar) findViewById(R.id.seekBarVideo); seekBarSound = (SeekBar) findViewById(R.id.seekBarSound); seekBarLight = (SeekBar) findViewById(R.id.seekBarLight); } Handler uiHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case 0: if (videoView.getDuration() > 0) { seekBarVideo.setMax(videoView.getDuration()); seekBarVideo.setProgress(videoView.getCurrentPosition()); } updateTextViewWithTimeFormat(tvCurrentTime, videoView.getCurrentPosition() / 1000); updateTextViewWithTimeFormat(tvTotalTime, videoView.getDuration() / 1000); tvVideoInfo.setText("duration:" + videoView.getDuration() + "\n"); tvVideoInfo.append("currentPosition:" + videoView.getCurrentPosition() + "\n"); updateTextViewWithTimeFormat(tvPlayTime, videoView.getCurrentPosition() / 1000); uiHandler.sendEmptyMessageDelayed(0, 200); break; } } }; //初始化資料 private void initData() { // videoView.setMediaController(mediaController); videoView.setVideoPath("http://www.jmzsjy.com/UploadFile/%E5%BE%AE%E8%AF%BE/%E5%9C%B0%E6%96%B9%E9%A3%8E%E5%91%B3%E5%B0%8F%E5%90%83%E2%80%94%E2%80%94%E5%AE%AB%E5%BB%B7%E9%A6%99%E9%85%A5%E7%89%9B%E8%82%89%E9%A5%BC.mp4"); videoView.start(); int screenWidth = MyUtils.getScreenWidth(this); int height = screenWidth / 4 * 3; FitScreenUtil.FixScreenXY(videoView, screenWidth, height); uiHandler.sendEmptyMessageDelayed(0, 200); tvVideoInfo.setText("duration:" + videoView.getDuration() + "\n"); tvVideoInfo.append("currentPosition:" + videoView.getCurrentPosition() + "\n"); seekBarVideo.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int i, boolean b) { } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { int seekPos = seekBar.getProgress(); videoView.seekTo(seekPos); } }); seekBarLight.setMax(255); seekBarLight.setProgress(screenBrightness); if (screenMode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) { setScreenMode(Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); } seekBarLight.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int i, boolean b) { } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { int seekPos = seekBar.getProgress(); setScreenBrightness(seekPos); } }); seekBarSound.setMax(15); seekBarSound.setProgress(soundAudio); seekBarSound.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int i, boolean b) { } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { int seekPos = seekBar.getProgress(); audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, seekPos, AudioManager.FLAG_PLAY_SOUND); } }); } private void setScreenMode(int value) { Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, value); } private void setScreenBrightness(int value) { Window w = getWindow(); WindowManager.LayoutParams l = w.getAttributes(); l.screenBrightness = value; w.setAttributes(l); Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, value); } @Override public void onBackPressed() { backPressed = true; setScreenBrightness(screenBrightness); setScreenMode(screenMode); audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, soundAudio, AudioManager.FLAG_PLAY_SOUND); super.onBackPressed(); } @Override public void onClick(View v) { super.onClick(v); switch (v.getId()) { case R.id.btnForword: // TODO: 16/9/6 break; case R.id.btnStop: videoView.pause(); break; case R.id.btnGoOn: videoView.start(); break; case R.id.btnNext: break; case R.id.btnQuickBack: videoView.seekTo(videoView.getCurrentPosition() - 1000 * 10); break; case R.id.btnQuickForword: videoView.seekTo(videoView.getCurrentPosition() + 1000 * 10); break; case R.id.btnGetCurrent: tvVideoInfo.append("當前進度:" + videoView.getCurrentPosition() + "\n"); tvVideoInfo.append("duration:" + videoView.getDuration() + "\n"); tvVideoInfo.append("currentPosition:" + videoView.getCurrentPosition() + "\n"); break; } } private void updateTextViewWithTimeFormat(TextView textView, int second) { int hh = second / 3600; int mm = second % 3600 / 60; int ss = second % 60; String stringTemp = null; if (0 != hh) { stringTemp = String.format("%02d:%02d:%02d", hh, mm, ss); } else { stringTemp = String.format("%02d:%02d", mm, ss); } textView.setText(stringTemp); } @Override protected void onResume() { super.onResume(); if (videoView.getCurrentPosition() > 0) { videoView.start(); } } @Override protected void onPause() { super.onPause(); if (backPressed || videoView.isBackgroundPlayEnabled()) { videoView.stopPlayback(); videoView.release(true); videoView.stopBackgroundPlay(); } else { videoView.stopBackgroundPlay(); } videoView.pause(); IjkMediaPlayer.native_profileEnd(); setScreenBrightness(screenBrightness); setScreenMode(screenMode); audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, soundAudio, AudioManager.FLAG_PLAY_SOUND); } @Override public ITrackInfo[] getTrackInfo() { if (videoView == null) return null; return videoView.getTrackInfo(); } @Override public int getSelectedTrack(int trackType) { if (videoView == null) return -1; return videoView.getSelectedTrack(trackType); } @Override public void selectTrack(int stream) { videoView.selectTrack(stream); } @Override public void deselectTrack(int stream) { videoView.deselectTrack(stream); } }

xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.fanyafeng.testijkplayer.activity.PlayerActivity"
    tools:showIn="@layout/activity_player">

    <!--需要v4或者v7包,不過一般as建立的工程都預設存在,此處採用滑動巢狀佈局,為了解決滑動衝突-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <com.fanyafeng.testijkplayer.widget.media.IjkVideoView
                android:id="@+id/videoView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_alignParentBottom="true"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/tvCurrentTime"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="播放時間:"
                    android:textColor="@android:color/white" />

                <SeekBar
                    android:id="@+id/seekBarVideo"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:layout_toLeftOf="@+id/tvTotalTime"
                    android:layout_toRightOf="@+id/tvCurrentTime" />

                <TextView
                    android:id="@+id/tvTotalTime"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_alignParentRight="true"
                    android:gravity="center"
                    android:text="總時間:"
                    android:textColor="@android:color/white" />

            </RelativeLayout>
        </RelativeLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="音量調節" />

        <SeekBar
            android:id="@+id/seekBarSound"
            android:layout_width="match_parent"
            android:layout_height="40dp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="亮度調節" />

        <SeekBar
            android:id="@+id/seekBarLight"
            android:layout_width="match_parent"
            android:layout_height="40dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/btnForword"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="onClick"
                android:text="上一個" />

            <Button
                android:id="@+id/btnStop"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="onClick"
                android:text="暫停" />

            <Button
                android:id="@+id/btnGoOn"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="onClick"
                android:text="繼續" />

            <Button
                android:id="@+id/btnNext"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="onClick"
                android:text="下一個" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/btnQuickBack"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="onClick"
                android:text="快退" />

            <Button
                android:id="@+id/btnGetCurrent"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="onClick"
                android:text="獲取當前進度" />

            <Button
                android:id="@+id/btnQuickForword"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="onClick"
                android:text="快進" />
        </LinearLayout>

        <TextView
            android:id="@+id/tvPlayTime"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="播放資訊" />

        <TextView
            android:id="@+id/tvVideoInfo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="視訊詳細資訊" />

    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

東西不是很多,而且感覺不難,命名也很規範就沒有去寫註釋,如果有不懂得感覺看原始碼就可以搞定了