當前位置:首頁 » 安卓系統 » android播放視頻代碼

android播放視頻代碼

發布時間: 2022-09-28 14:47:50

『壹』 Android 使用VideoView實現簡單視頻播放

最近項目里要寫一個簡單的視頻播放頁面,先上一下效果圖吧

因為考慮我們的需求比較簡單,就沒有使用三方的框架,採用了原生的VideoView。
因為很長時間沒用過原生的VideoView了,大部分採用的都是餃子播放器或者別的三方框架,所以本篇文章主要用於自己復習,技術含量不高(嘿嘿)!

『貳』 安卓開發怎麼點擊按鈕就播放視頻videoview

Android使用VideoView實現VideoPlayer 在Android系統中,是通過MediaPalyer類播放媒體文件的(包括視頻和音頻)。雖然這個類已經比較簡單了,但是還需要控制各種狀態,對於視頻還需要設置輸出窗口,還是需要仔細研究的。為了避免這些麻煩事兒,Android框架提供了VideoView類來封MediaPalyer,這個VideoView類非常好用。Android自帶的程序Gallery也是用VideoView實現的。 通過VideoView播放視頻的步驟: 1、在界面布局文件中定義VideoView組件,或在程序中創建VideoView組件 2、調用VideoView的如下兩個方法來載入指定的視頻 setVidePath(String path):載入path文件代表的視頻 setVideoURI(Uri uri):載入uri所對應的視頻 3、調用VideoView的start()、stop()、psuse()方法來控制視頻的播放 VideoView通過與MediaController類結合使用,開發者可以不用自己控制播放與暫停 簡單實例: activity_main/apk/res/android" xmlns:tools="schemas/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <VideoView android:id="@+id/video1" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout> Activity 控制代碼: package com.Uri; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.MediaController; import android.widget.VideoView; public class MainActivity extends Activity { private VideoView video1; MediaController mediaco; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); video1=(VideoView)findViewById(R.id.video1); mediaco=new MediaController(this); File file=new File("/mnt/sdcard/通話錄音/1.mp4"); if(file.exists()){ //VideoView與MediaController進行關聯 video1.setVideoPath(file.getAbsolutePath()); video1.setMediaController(mediaco); mediaco.setMediaPlayer(video1); //讓VideiView獲取焦點 video1.requestFocus(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }

『叄』 android怎樣播放assets中的視頻,關鍵代碼video.setVideoPath("路徑")對嗎我用了出錯了.

assets文件夾裡面的文件都是保持原始的文件格式,需要用AssetManager以位元組流的形式讀取文件。
video.setVideoPath("路徑")是讀取不到文件的
assets的讀取方式:
1. 先在Activity裡面調用getAssets() 來獲取AssetManager引用。
2. 再用AssetManager的open(String fileName, int accessMode) 方法則指定讀取的文件以及訪問模式就能得到輸入流InputStream。
3. 然後就是用已經open file 的inputStream讀取文件,讀取完成後記得inputStream.close() 。
4.調用AssetManager.close() 關閉AssetManager。

需要注意的是,來自Resources和Assets 中的文件只可以讀取而不能進行寫的操作

『肆』 android 實現播放網路視頻

沒有調start(),vv.setVideoURI(uri);後要調用vv.start(),才可以播放,這裡面有好幾個介面,pause,resunme,stop你看下api,建議看下Mediaplayer播放的狀態機,對你理解多媒體播放有好處。

『伍』 請問怎麼把一個android的視頻播放界面在代碼中設置為橫向的全屏並不能滾動

在Manifest中設置activity為橫向,主題設置為全屏就行了
不能滾動是你在配置xml的時候設定好fillparent就行了

『陸』 android怎樣播放assets中的視頻,關鍵代碼video.setVideoPath("路徑")對嗎我用了出錯了.

不能在非同步線程刷新主線程UI解決法:this.runOnUiThread(newRunnable(){@Overridepublicvoidrun(){}});利用handler

『柒』 求一份Android視頻播放器源碼

怎麼發給你
package wyf.zcl;

import java.util.HashMap;

import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MyActivity extends Activity {
/** Called when the activity is first created. */
SoundPool sp; //得到一個聲音池引用
HashMap<Integer,Integer> spMap; //得到一個map的引用
Button b1; //聲音播放控制按鈕
Button b1Pause; //聲音暫停控制按鈕
Button b2; //聲音播放控制按鈕
Button b2Pause; //聲音暫停控制按鈕
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initSoundPool(); //初始化聲音池
b1=(Button)findViewById(R.id.Button01);//聲音播放控制按鈕實例化
b2=(Button)findViewById(R.id.Button02);//聲音播放控制按鈕實例化
b1Pause=(Button)findViewById(R.id.Button1Pause);
b2Pause=(Button)findViewById(R.id.Button2Pause);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(1,1); //播放第一首音效,循環一遍
Toast.makeText(MyActivity.this, "播放音效1", Toast.LENGTH_SHORT).show();
}});
b1Pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sp.pause(spMap.get(1));
Toast.makeText(MyActivity.this, "暫停音效1", Toast.LENGTH_SHORT).show();
}});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(2,1); //播放第二首音效,循環一遍
Toast.makeText(MyActivity.this, "播放音效2", Toast.LENGTH_SHORT).show();
}});
b2Pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sp.pause(spMap.get(2));
Toast.makeText(MyActivity.this, "暫停音效2", Toast.LENGTH_SHORT).show();
}});
}
public void initSoundPool(){ //初始化聲音池
sp=new SoundPool(
5, //maxStreams參數,該參數為設置同時能夠播放多少音效
AudioManager.STREAM_MUSIC, //streamType參數,該參數設置音頻類型,在游戲中通常設置為:STREAM_MUSIC
0 //srcQuality參數,該參數設置音頻文件的質量,目前還沒有效果,設置為0為默認值。
);
spMap=new HashMap<Integer,Integer>();
spMap.put(1, sp.load(this, R.raw.attack02, 1));
spMap.put(2, sp.load(this, R.raw.attack14, 1));
}
public void playSound(int sound,int number){ //播放聲音,參數sound是播放音效的id,參數number是播放音效的次數
AudioManager am=(AudioManager)this.getSystemService(this.AUDIO_SERVICE);//實例化AudioManager對象
float audioMaxVolumn=am.getStreamMaxVolume(AudioManager.STREAM_MUSIC); //返回當前AudioManager對象的最大音量值
float audioCurrentVolumn=am.getStreamVolume(AudioManager.STREAM_MUSIC);//返回當前AudioManager對象的音量值
float volumnRatio=audioCurrentVolumn/audioMaxVolumn;
sp.play(
spMap.get(sound), //播放的音樂id
volumnRatio, //左聲道音量
volumnRatio, //右聲道音量
1, //優先順序,0為最低
number, //循環次數,0無不循環,-1無永遠循環
1 //回放速度 ,該值在0.5-2.0之間,1為正常速度
);
}
}
要在資源中加聲音文件哦

『捌』 求Android實現邊下邊播代碼,思路也行 求大神賞給小弟吧

1) 播放器在播放磁力鏈、或者torrrent文件時,調用產品的P2P引擎,p2p引擎裡面通過torrent或者磁力鏈信息,去通過tracker、DHT尋找周圍的peer鄰居,然後獲取數據。
2) p2p獲取到的數據,通過播放器可以支持的協議,返回給播放器,這就是一個標準的視頻文件,所以它可以正常播放
3) 播放器在拖動時,p2p引擎自動切換下載位置,並返回數據。以上這些是不分Android、IOS還是PC的,只是在移動端可能下載速度、檢測是否WIFI之類的有些策略上的差異而已

『玖』 android程序播放音樂和改成放視頻怎麼弄,需要在代碼中怎麼更改在哪裡改

首先新建一個項目命名為Mp3Demo(這個看個人喜好),Activity命名為Mp3Demo。
1.接下來在Mp3Demo.java中輸入以下代碼:

package com.example.mp3demo;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Mp3Demo extends Activity
{ //聲名變數
private Button start=null;
private Button pause=null;
private Button stop=null;
private TextView state=null;
private MediaPlayer mp3;
private Boolean flag=false; //設置標記,false表示正在播放
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_mp3_demo);
//取得各按鈕組件
start=(Button) super.findViewById(R.id.start);
pause=(Button) super.findViewById(R.id.pause);
stop=(Button) super.findViewById(R.id.stop);
state=(TextView)super.findViewById(R.id.state);
//為每個按鈕設置單擊事件
start.setOnClickListener(new OnClickListenerStart());
pause.setOnClickListener(new OnClickListenerPause());
stop.setOnClickListener(new OnClickListenerStop());
mp3= new MediaPlayer(); //創建一個MediaPlayer對象
//在res下新建一個raw文件夾把一首歌放到此文件夾中並用英文命名
mp3 = MediaPlayer.create(Mp3Demo.this,R.raw.sky);
}
//各按鈕單擊事件的實現如下
//開始播放
private class OnClickListenerStart implements OnClickListener{
//implementsOnClickListener為實現OnClickListener介面
@Override
//重寫onClic事件
public void onClick(View v)
{
//執行的代碼,其中可能有異常。一旦發現異常,則立即跳到catch執行。否則不會執行catch裡面的內容
try
{
if (mp3!=null)
{
mp3.stop();
}
mp3.prepare(); //進入到准備狀態
mp3.start(); //開始播放
state.setText("Playing"); //改變輸出信息為「Playing」,下同
} catch (Exception e)
{
state.setText(e.toString());//以字元串的形式輸出異常
e.printStackTrace(); //在控制台(control)上列印出異常
}
}
}
//暫停播放
private class OnClickListenerPause implements OnClickListener{
@Override
public void onClick(View v)
{
try
{
if (flag==false) //若flag為false,則表示此時播放器的狀態為正在播放
{
mp3.pause();
flag=true;
state.setText("pause");
}
else if(flag==true){
mp3.start(); //開始播放
flag=false; //重新設置flag為false
state.setText("Playing");
}
} catch (Exception e)
{
state.setText(e.toString());
e.printStackTrace();
}
}
}
//停止播放
private class OnClickListenerStop implements OnClickListener{
@Override
public void onClick(View v)
{
try
{
if (mp3!=null)
{
mp3.stop();
state.setText("stop");
}
} catch (Exception e)
{
state.setText(e.toString());
e.printStackTrace();
}
}
}
//重寫暫停狀態事件
protected void onPause(){
try
{
mp3.release(); //釋放音樂資源
} catch (Exception e)
{
state.setText(e.toString());
e.printStackTrace();
}
super.onPause();
}
}

2.按下來是定義布局文件,代碼如下:

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
android:id="@+id/state"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start"/>
android:id="@+id/pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="pause"/>
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="stop"/>

熱點內容
磁帶存儲價格 發布:2024-04-24 21:04:22 瀏覽:900
誤刪除文件夾恢復工具 發布:2024-04-24 20:31:57 瀏覽:383
php介面編寫 發布:2024-04-24 20:31:06 瀏覽:68
怎麼架設雙線伺服器 發布:2024-04-24 20:25:55 瀏覽:639
通易雲源碼 發布:2024-04-24 20:14:55 瀏覽:963
安卓手機卸載更新什麼意思 發布:2024-04-24 19:29:35 瀏覽:228
文件des加密 發布:2024-04-24 19:24:20 瀏覽:705
魔獸世界data文件夾 發布:2024-04-24 19:24:13 瀏覽:214
蘋果手機怎麼清空緩存 發布:2024-04-24 19:23:38 瀏覽:893
微信密碼沒有手機號如何找回 發布:2024-04-24 19:18:20 瀏覽:875