forked from hzuapps/android-labs-2017
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
375 additions
and
0 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
...dLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903118/MyAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package edu.hzuapps.androidlab.Net1414080903118; | ||
|
||
import android.content.Context; | ||
import android.graphics.BitmapFactory; | ||
import android.util.Log; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.BaseAdapter; | ||
import android.widget.ImageView; | ||
import android.widget.TextView; | ||
|
||
import com.example.administrator.video.R; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by Administrator on 2017/5/7. | ||
*/ | ||
|
||
public class MyAdapter extends BaseAdapter { | ||
|
||
Context context; | ||
List<VideoInfo> list; | ||
|
||
public MyAdapter(Context context, List<VideoInfo> list) { | ||
this.context = context; | ||
this.list = list; | ||
} | ||
|
||
@Override | ||
public int getCount() { | ||
return list.size(); | ||
} | ||
|
||
@Override | ||
public Object getItem(int position) { | ||
return list.get(position).getPath(); | ||
} | ||
|
||
@Override | ||
public long getItemId(int position) { | ||
return position; | ||
} | ||
|
||
@Override | ||
public View getView(int position, View convertView, ViewGroup parent) { | ||
Holder holder = null; | ||
if (convertView == null) { | ||
convertView = LayoutInflater.from(context).inflate(R.layout.menu_item, null); | ||
holder = new Holder(); | ||
holder.iv = (ImageView) convertView.findViewById(R.id.iv_item); | ||
holder.tvTitle = (TextView) convertView.findViewById(R.id.tv_item_title); | ||
holder.tvDuration = (TextView) convertView.findViewById(R.id.tv_item_duration); | ||
convertView.setTag(holder); | ||
} else { | ||
holder = (Holder) convertView.getTag(); | ||
} | ||
holder.tvTitle.setText(list.get(position).getTitle()); | ||
long duration = Long.parseLong(list.get(position).getDuration()); | ||
duration = duration / 1000; | ||
StringBuilder builder = new StringBuilder("时长:"); | ||
int hour = (int) (duration / 3600); | ||
builder.append(hour).append("时"); | ||
int minuter = (int) ((duration % 3600) / 60); | ||
builder.append(minuter).append("分"); | ||
int second = (int) (duration % 60); | ||
builder.append(second).append("秒"); | ||
holder.tvDuration.setText(builder.toString()); | ||
if(list.get(position).getThumbPath()!=null){ | ||
File file = new File(list.get(position).getThumbPath()); | ||
holder.iv.setImageBitmap(BitmapFactory.decodeFile(file.getAbsolutePath())); | ||
} | ||
return convertView; | ||
} | ||
|
||
private class Holder { | ||
TextView tvTitle; | ||
TextView tvDuration; | ||
ImageView iv; | ||
} | ||
|
||
} |
102 changes: 102 additions & 0 deletions
102
...java/edu/hzuapps/androidlabs/homeworks/net1414080903118/Net1414080903118MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package edu.hzuapps.androidlab.Net1414080903118; | ||
|
||
import android.content.Intent; | ||
import android.database.Cursor; | ||
import android.provider.MediaStore; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import android.util.Log; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.widget.AdapterView; | ||
import android.widget.ListView; | ||
import android.widget.TextView; | ||
import android.widget.Toast; | ||
|
||
import com.example.administrator.video.R; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Net1414080903118MainActivity extends AppCompatActivity { | ||
|
||
private static final String TAG = "MainActivity"; | ||
Cursor cursor; | ||
ListView lv; | ||
|
||
public List<VideoInfo> sysVideoList = new ArrayList<>(); | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_net1414809003118_main); | ||
lv= (ListView) findViewById(R.id.lv); | ||
setVideoList(); | ||
final MyAdapter adapter=new MyAdapter(this,sysVideoList); | ||
lv.setAdapter(adapter); | ||
lv.setEmptyView(LayoutInflater.from(this).inflate(R.layout.empty_list,null)); | ||
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { | ||
@Override | ||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | ||
Intent intent=new Intent(Net1414080903118MainActivity.this,Net1414080903118PlayActivity.class); | ||
intent.putExtra("path",(String)adapter.getItem(position)); | ||
startActivity(intent); | ||
} | ||
}); | ||
} | ||
|
||
private void setVideoList() { | ||
// MediaStore.Video.Thumbnails.DATA:视频缩略图的文件路径 | ||
String[] thumbColumns = {MediaStore.Video.Thumbnails.DATA, | ||
MediaStore.Video.Thumbnails.VIDEO_ID}; | ||
|
||
// MediaStore.Video.Media.DATA:视频文件路径; | ||
// MediaStore.Video.Media.DISPLAY_NAME : 视频文件名,如 testVideo.mp4 | ||
// MediaStore.Video.Media.TITLE: 视频标题 : testVideo | ||
String[] mediaColumns = {MediaStore.Video.Media._ID, | ||
MediaStore.Video.Media.DATA, MediaStore.Video.Media.TITLE, | ||
MediaStore.Video.Media.MIME_TYPE, | ||
MediaStore.Video.Media.DURATION, | ||
MediaStore.Video.Media.DISPLAY_NAME}; | ||
|
||
cursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, | ||
mediaColumns, null, null, null); | ||
|
||
if (cursor == null) { | ||
Toast.makeText(Net1414080903118MainActivity.this, "没有找到可播放视频文件", Toast.LENGTH_LONG).show(); | ||
return; | ||
} | ||
if (cursor.moveToFirst()) { | ||
do { | ||
VideoInfo info = new VideoInfo(); | ||
int id = cursor.getInt(cursor | ||
.getColumnIndex(MediaStore.Video.Media._ID)); | ||
Cursor thumbCursor = managedQuery( | ||
MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI, | ||
thumbColumns, MediaStore.Video.Thumbnails.VIDEO_ID | ||
+ "=" + id, null, null); | ||
if (thumbCursor.moveToFirst()) { | ||
info.setThumbPath(thumbCursor.getString(thumbCursor | ||
.getColumnIndex(MediaStore.Video.Thumbnails.DATA))); | ||
} | ||
info.setPath(cursor.getString(cursor | ||
.getColumnIndexOrThrow(MediaStore.Video.Media.DATA))); | ||
info.setTitle(cursor.getString(cursor | ||
.getColumnIndexOrThrow(MediaStore.Video.Media.TITLE))); | ||
info.setDisplayName(cursor.getString(cursor | ||
.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME))); | ||
info.setDuration(cursor.getString(cursor | ||
.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION))); | ||
Log.d(TAG, "DisplayName:" + info.getDisplayName()); | ||
info.setMimeType(cursor | ||
.getString(cursor | ||
.getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE))); | ||
String displayName=info.getDisplayName(); | ||
String s=displayName.substring(displayName.indexOf(".")+1); | ||
if(s.equals("mp4")||s.equals("3pg")) | ||
sysVideoList.add(info); | ||
} while (cursor.moveToNext()); | ||
} | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
...java/edu/hzuapps/androidlabs/homeworks/net1414080903118/Net1414080903118PlayActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package edu.hzuapps.androidlab.Net1414080903118; | ||
|
||
import android.content.Intent; | ||
import android.content.pm.ActivityInfo; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import android.util.Log; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.MediaController; | ||
import android.widget.VideoView; | ||
|
||
import com.example.administrator.video.R; | ||
|
||
import java.io.File; | ||
|
||
public class Net1414080903118PlayActivity extends AppCompatActivity { | ||
|
||
VideoView videoView; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_net1414080903118_play); | ||
getSupportActionBar().hide(); | ||
String path= getIntent().getStringExtra("path"); | ||
videoView= (VideoView) findViewById(R.id.video); | ||
File file=new File(path); | ||
Log.d("path",file.getAbsolutePath()); | ||
videoView.setVideoPath(file.getAbsolutePath()); | ||
MediaController mediaController=new MediaController(this); | ||
mediaController.setMediaPlayer(videoView); | ||
videoView.setMediaController(mediaController); | ||
if(!videoView.isPlaying()){ | ||
videoView.start(); | ||
} | ||
} | ||
|
||
@Override | ||
protected void onResume() { | ||
super.onResume(); | ||
if(getRequestedOrientation()!= ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){ | ||
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); | ||
} | ||
} | ||
|
||
@Override | ||
protected void onDestroy() { | ||
super.onDestroy(); | ||
if(videoView!=null){ | ||
videoView.suspend(); | ||
} | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...dLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903118/VideoInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package edu.hzuapps.androidlab.Net1414080903118; | ||
|
||
/** | ||
* Created by Administrator on 2017/5/7. | ||
*/ | ||
public class VideoInfo { | ||
|
||
String thumbPath; | ||
String path; | ||
String title; | ||
String mimeType; | ||
String displayName; | ||
String duration; | ||
|
||
public String getDuration() { | ||
return duration; | ||
} | ||
|
||
public void setDuration(String duration) { | ||
this.duration = duration; | ||
} | ||
|
||
public String getDisplayName() { | ||
return displayName; | ||
} | ||
|
||
public void setDisplayName(String displayName) { | ||
this.displayName = displayName; | ||
} | ||
|
||
public String getPath() { | ||
return path; | ||
} | ||
|
||
public void setPath(String path) { | ||
this.path = path; | ||
} | ||
|
||
public String getMimeType() { | ||
return mimeType; | ||
} | ||
|
||
public void setMimeType(String mimeType) { | ||
this.mimeType = mimeType; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
public String getThumbPath() { | ||
return thumbPath; | ||
} | ||
|
||
public void setThumbPath(String thumbPath) { | ||
this.thumbPath = thumbPath; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return thumbPath+"-"+path+"-"+title+"-"+mimeType+"-"+displayName+"\n"; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
AndroidLabs/app/src/main/res/layout/activity_net1414080903118_play.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
<VideoView | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:id="@+id/video"/> | ||
</RelativeLayout> |
12 changes: 12 additions & 0 deletions
12
AndroidLabs/app/src/main/res/layout/activity_net1414809003118_main.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
<ListView | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:id="@+id/lv"/> | ||
|
||
|
||
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<TextView xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:gravity="center" | ||
android:textSize="18sp" | ||
android:padding="50dp" | ||
android:text="未找到符合格式的視頻文件,本播放器仅支持3pg,mp4格式的视频"> | ||
|
||
</TextView> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:orientation="horizontal" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:padding="15dp"> | ||
|
||
<ImageView | ||
android:src="@mipmap/ic_launcher" | ||
android:layout_width="70dp" | ||
android:layout_height="80dp" | ||
android:id="@+id/iv_item"/> | ||
|
||
<LinearLayout | ||
android:layout_marginLeft="15dp" | ||
android:layout_width="0dp" | ||
android:layout_weight="1" | ||
android:layout_height="wrap_content" | ||
android:orientation="vertical"> | ||
<TextView | ||
android:id="@+id/tv_item_title" | ||
android:textSize="16sp" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" /> | ||
|
||
<TextView | ||
android:layout_marginTop="10dp" | ||
android:id="@+id/tv_item_duration" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:textSize="14sp"/> | ||
|
||
|
||
</LinearLayout> | ||
|
||
</LinearLayout> |