Ćwiczenie 4.1 - Android - Odtwarzacz plików wideo


Odtwarzacz plików wideo

Na rysunku 1 pokazana jest struktura projektu

 Rysunek 1. Struktura projektu

Plik MainActivity.java

package com.example.playaudio01;

import...
 
public class MainActivity extends Activity {

VideoView simpleVideoView;
MediaController mediaControls;

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

simpleVideoView = (VideoView) findViewById(R.id.simpleVideoView);

if (mediaControls == null) {

mediaControls = new MediaController(MainActivity.this);
mediaControls.setAnchorView(simpleVideoView);
}

simpleVideoView.setMediaController(mediaControls);

simpleVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video));

simpleVideoView.start();

simpleVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
Toast.makeText(getApplicationContext(), "Thank You...!!!", Toast.LENGTH_LONG).show();
}
});
simpleVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Toast.makeText(getApplicationContext(), "Oops An Error Occur While Playing Video...!!!",
Toast.LENGTH_LONG).show();
return false;
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();

if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

 Plik AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="example.abhiandroid.videoviewexample">

<uses-permission android:name="ANDROID.PERMISSION.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

Plik strings.xml

resources>
<string name="app_name">VideoViewExample</string>

<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
</resources>

Na rysunku 2 pokazany jest widok ekranu na etapie projektowania.

 
Rysunek 2. Widok projektu ekranu odtwarzacza audio

Plik activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="example.abhiandroid.videoviewexample">

<uses-permission android:name="ANDROID.PERMISSION.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

Testowy plik wideo: Video